import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2022-08-02
 * Time: 14:57
 */
public class Test_0801 {


    public static int[] double_numbeir2(int[] arr) {
        //new 一个新数组，防止改变原数组
        int[] tmp = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            tmp[i] = arr[i] * 2;
        }
        return tmp;
    }

    public static void double_number1(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] * 2;
        }
    }

    //改变原有数组元素的值,每个元素都乘2
    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        double_number1(array);
        int[] ret = double_numbeir2(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static float average_number(float[] arr) {
        float sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum / arr.length;
    }

    public static void main2(String[] args) {
        //求数组的平均值
        float[] array = {1.2f, 2.8f, 3.3f, 4.7f, 5.0f};
        float ret = average_number(array);
        System.out.println(ret);
    }

    public static void swap_parity(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            //左边寻找到偶数停止 -- 需要防止全是奇数时，left越界
            while (arr[left] % 2 != 0 && left < right) {
                left++;
            }
            //右边寻找到奇数停止 -- 需要防止右边全是偶数时，right越界
            while (arr[right] % 2 == 0 && left < right) {
                right--;
            }
            //交换奇偶
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }

    public static void main3(String[] args) {
        //奇数位于偶数之前
        int[] array = {1, 2, 7, 3, 6, 8, 9, 6, 8};
        swap_parity(array);
        System.out.println(Arrays.toString(array));
    }

    public static int binary_find(int[] arr, int k) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int mid = (left + right) / 2;
            //所找元素在中间元素的左边
            if (arr[mid] > k) {
                right = mid - 1;
            } else if (arr[mid] < k) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main4(String[] args) {
        //给定一个有序整型数组, 实现二分查找
        int[] array = {1, 3, 6, 7, 9};
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ret = binary_find(array, n);
        if (ret != -1) {
            System.out.println("找到了下标是：" + ret);
        } else {
            System.out.println("数组中无该元素！");
        }
    }

    //暴力求解
    public static int[] to_number_and(int[] arr, int k) {
        for (int i = 0; i < arr.length; i++) {
            //如果第一次不满足，下一次则从第二个数开始 -- 求和优化
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] + arr[j] == k) {
                    //直接返回 new一个数组
                    return new int[]{i, j};
                }
            }
        }
        //若不满足 返回空数组
        return new int[0];
    }

    public static void main5(String[] args) {
        //请你在该数组中找出 和为目标值 target 的那 两个 整数，并返回它们的数组下标。
        int[] array = {1, 3, 5, 7, 6, 9};
        Scanner scanner = new Scanner(System.in);
        int target = scanner.nextInt();
        int[] ret = to_number_and(array, target);
        System.out.println(Arrays.toString(ret));
    }

    public static int find_number(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum ^= arr[i];
        }
        return sum;
    }

    public static int[] find_number1(int[] arr) {
        int sum = 0;
        int num1 = 0;
        int num2 = 0;
        //全部异或  -- 异或后为不同两数的结果
        for (int i = 0; i < arr.length; i++) {
            sum ^= arr[i];
        }
        //寻找异或后的结果第一次出现1的地方 -- 由于不同两数异或 不同位为1 ，即可将不同两数分开
        int j = 0;
        for (j = 0; j < 32; j++) {
            if (((sum >> j) & 1) == 1) {
                break;
            }
        }
        //根据第 j 位是否为1的不同，将其分为两组并单独异或找出一组中不一样的那个
        for (int i = 0; i < arr.length; i++) {
            if (((arr[i] >> j) & 1) == 1) {
                num1 ^= arr[i];
            } else {
                num2 ^= arr[i];
            }
        }
        return new int[]{num1, num2};
    }

    public static void main6(String[] args) {
        //给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次
        int[] array = {1, 1, 2, 2, 3, 4, 4};
        int[] arrar1 = {1, 1, 2, 3, 4, 5, 5, 2};
        int[] ret1 = find_number1(arrar1);
        System.out.println("两个不同的元素是：" + Arrays.toString(ret1));
        int ret = find_number(array);
        System.out.println("不同元素是：" + ret);
    }

    public static int most_of_the_elements(int[] arr) {
        //投票法 -- 相同投一票 不同扣一票
        int tmp = arr[0];
        for (int i = 0; i < arr.length; i++) {
            int count = 0;
            if (tmp == arr[i]) {
                count++;
            } else {
                count--;
            }
            if (count == 0) {
                tmp = arr[i + 1];
            }
        }
        return tmp;
    }

    public static int sort_most_elements(int[] arr) {
        //排序取巧
        Arrays.sort(arr);
        return arr[arr.length / 2];
    }

    public static void main7(String[] args) {
        //给定一个大小为 n 的数组，找到其中的多数元素--出现次数大于 n/2  10个元素则至少出现6次
        int[] array = {1, 1, 1, 1, 3, 4, 1, 1, 1, 2, 3, 4};
        int ret = most_of_the_elements(array);
        int ret1 = sort_most_elements(array);
        System.out.println("多数元素为：" + ret);
        System.out.println("多数元素为：" + ret1);
    }

    public static boolean three_odd(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 != 0) {
                count++;
            } else {
                count = 0;
            }
            if (count == 3) {
                return true;
            }
        }
        return false;
    }

    public static void main8(String[] args) {
        //数组中是否存在连续的三个奇数
        int[] array = {1, 2, 4, 5, 77, 9};
        boolean ret = three_odd(array);
        System.out.println(ret);
    }

    public static void main9(String[] args) {
        //猜数字
        Scanner scanner = new Scanner(System.in);
        int number = (int) (Math.random() * 100); //左闭右开区间
        int input = 0;
        do {
            System.out.println(number);
            System.out.println("请输入数字：");
            input = scanner.nextInt();
            if (input > number) {
                System.out.println("猜大了，试着猜小一点吧！");
            } else if (input < number) {
                System.out.println("猜小了，试着猜大一点吧！");
            } else {
                System.out.println("恭喜你，猜对了！");
                break;
            }
        } while (true);
    }
    public static int fib(int n) {
        int fib1 = 0;
        int fib2 = 1;
        int ret = 0;
        while(ret < n) {
            ret = fib1 + fib2;
            fib1 = fib2;
            fib2 = ret;

            if(ret > n) {
                break;
            }

        }
        return Math.min((fib2-n),(n-fib1));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()) {
            int N = scanner.nextInt();
            System.out.println(fib(N));
        }
    }
}
